package com.pets.web.service.impl;

import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.pets.common.utils.StringUtils;
import com.pets.web.entity.PetsDelivery;
import com.pets.web.entity.PetsGoods;
import com.pets.web.mapper.PetsDeliveryMapper;
import com.pets.web.service.IPetOrdersService;
import com.pets.web.service.IPetsDeliveryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 发货管理 服务实现类
 * </p>
 *
 * @author zhurenwang
 * @since 2024-05-26
 */
@Service
public class PetsDeliveryServiceImpl extends ServiceImpl<PetsDeliveryMapper, PetsDelivery> implements IPetsDeliveryService {

    @Autowired
    private IPetOrdersService petOrdersService;

    @Override
    public List<PetsDelivery> list(PetsDelivery petsDelivery) {
        MPJLambdaWrapper<PetsDelivery> wrapper = JoinWrappers.lambda(PetsDelivery.class)
                .selectAll(PetsDelivery.class)
                .selectAs(PetsGoods::getName, PetsDelivery::getGoodsName)
                .leftJoin(PetsGoods.class, PetsGoods::getId, PetsDelivery::getGoodsId)
                .eq(Objects.nonNull(petsDelivery.getType()), PetsDelivery::getType, petsDelivery.getType())
                .like(StringUtils.isNotEmpty(petsDelivery.getTitle()), PetsDelivery::getTitle, petsDelivery.getTitle());
        return baseMapper.selectJoinList(PetsDelivery.class, wrapper);
    }

    @Override
    public int deleteById(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(PetsDelivery petsDelivery) {
        petOrdersService.updateByIds(petsDelivery.getOrderIds());
        return baseMapper.insert(petsDelivery);
    }

    @Override
    public List<PetsDelivery> selectByOrderId(Integer orderId) {
        return baseMapper.selectByOrderId(orderId);
    }
}
